package com.example.shunsu.demo;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.example.shunsu.pojo.Custom;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class DemoMain {
    private String toPath = "E:\\zz\\";

    String packageName = "jk";
    String comments = "项目档案补录";
    String className = "xmdabl";
    String tableName = "JK_XMXX";
    String lcmc = "";// YW_MXZXWBGSP
    String dqsj = "2025年4月29日 上午10:00:00";
    String zllb = "dazl"; // dazl
    String xtgnfid = "ZH";//系统功能fid ZH
    boolean isYQxsq = false; // 是否有取消申请

    public static void main(String[] args) {
        DemoMain main = new DemoMain();
        main.generateAction();

        // select max(sxh) from gg_xtgn where sjid='CGSQ'
        long xtgnSxh = 840;
        // select  max(sxh)  from gg_dmnr where dmlb='SPLCFL' order by sxh
        int lcflSxh = 1780;
        main.generateSql(xtgnSxh, lcflSxh);
    }

    public void generateAction() {
        Map<String, Object> mParam = this.mParam(packageName, comments, className, tableName, lcmc, dqsj);

        this.exe(mParam, "Facade.ftl", StrUtil.upperFirst(className) + "Facade.java");
        this.exe(mParam, "Action.ftl", StrUtil.upperFirst(className) + "Action.java");
        this.exe(mParam, "MainFrame.ftl", "MainFrame.jsp");
        this.exe(mParam, "IcList.ftl", "IcList" + StrUtil.upperFirst(className) + ".jsp");
        this.exe(mParam, "Edit.ftl", "Edit" + StrUtil.upperFirst(className) + ".jsp");
        this.exe(mParam, "ViewSh.ftl", "View" + StrUtil.upperFirst(className) + ".jsp");
        this.exe(mParam, "Even.ftl", lcmc + "_EVEN.java");

        this.exe(mParam, "HTMLMainFrame.ftl", "MainFrame.html");
        this.exe(mParam, "HTMLIcList.ftl", "IcList" + StrUtil.upperFirst(className) + ".html");
        this.exe(mParam, "HTMLEdit.ftl", "Edit" + StrUtil.upperFirst(className) + ".html");
        this.exe(mParam, "HTMLViewSh.ftl", "View" + StrUtil.upperFirst(className) + ".html");

        if (StrUtil.isNotEmpty(lcmc)) {
            mParam.put("shTitle", "审核");
            this.exe(mParam, "ViewSh.ftl", "ShFrame.jsp");
            this.exe(mParam, "HTMLViewSh.ftl", "ShFrame.html");
            mParam.put("shTitle", "审核确认");
            this.exe(mParam, "ViewSh.ftl", "ShQrFrame.jsp");
            this.exe(mParam, "HTMLViewSh.ftl", "ShQrFrame.html");
            this.exe(mParam, "ShEdit.ftl", "ShEditFrame.jsp");
            this.exe(mParam, "HTMLShEdit.ftl", "ShEditFrame.html");
        }
    }


    /**
     * 装载数据
     */
    public Map<String, Object> mParam(String packageName, String comments, String className, String tableName, String lcmc, String dqsj) {
        Map<String, Object> param = new HashMap<>();
        param.put("packageName", packageName);
        param.put("comments", comments);
        param.put("className", className);
        param.put("tableName", tableName);
        param.put("lcmc", lcmc);
        param.put("dqsj", dqsj);
        param.put("zllb", zllb);
        param.put("isYQxsq", isYQxsq);
        return param;
    }


    public void exe(Map<String, Object> root, String template, String fileName) {
        try {
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_26);

            String ftlPath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("templates")).getPath() + "/ftl";
            // 配置模板位置
            cfg.setDirectoryForTemplateLoading(new File(ftlPath));
            cfg.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_26));

            Template temp = cfg.getTemplate(template);

            // 生成文件的位置
            File foload = new File(toPath);
            if(!foload.exists()){
               foload.mkdir();
            }
            File file = new File(toPath + fileName + ".txt");
            FileUtil.del(file);

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8));

            root.put("cus", new Custom());// 自定义函数
            temp.process(root, bw);

            FileUtil.copy(toPath + fileName + ".txt", toPath + "\\z\\" + fileName, true);

            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成sql
     */
    public void generateSql(long xtgnSxh, int lcflSxh) {
        // 系统功能
        String fid = "暂无=====================";
        if (StrUtil.isEmpty(xtgnfid)) {
            if ("cg".equals(packageName)) {
                fid = "CGSQ";
            } else if ("zh".equals(packageName)) {
                fid = "ZH";
            }
        } else {
            fid = xtgnfid;
        }

        String xtgnUuid = UUID.randomUUID().toString(true);
        String xtgnSql = "INSERT INTO GG_XTGN (GNID, SJID, GNMC, LJDZ, GNJC, GNLB, GNCS, IMG1, IMG2, SYBZ, BZSM, SXH, XSYS)\n" +
                "values ('" + xtgnUuid + "', '" + fid + "', '" + comments + "', '/" + packageName + "/" + className + "/showFrame.do', 2, 0, null, null, null, 1, '" + comments + "', " + xtgnSxh + ", null);\n";

        // 审批流程分类
        String splcSql = "INSERT INTO GG_DMNR (ID, DMLB, FID, DMMC, DMBS, BZSM, DMQC, JC, T01, N01, D01, T02, N02, D02, T03, N03, D03, T04, N04, D04, T05, N05, D05, T06, N06, D06, T07, N07, D07, T08, N08, D08, T09, N09, D09, T10, N10, D10, SXH, SYBZ, YZJDBZ, N11, N12, N13, N14, N15, N16, N17) \n" +
                "VALUES ('" + UUID.randomUUID().toString(true) + "', 'SPLCFL', '0', '" + comments + "审批', '" + className + "sp', '" + comments + "审批', '" + comments + "审批', 1, null, 0, 0.0000, null, 0, 0.0000, null, 0, 0.0000, null, 0, 0.0000, null, 0, 0.0000, null, 0, 0.0000, null, 0, 0.0000, null, 0, 0.0000, null, 0, 0.0000, null, 0, 0.0000, " + lcflSxh + ", 1, 1, 0, 0, 0, 0, 0, 0, 0);\n";

        // 流程表单
        String shEditId = UUID.randomUUID().toString(true);
        String shQrId = UUID.randomUUID().toString(true);
        String shId = UUID.randomUUID().toString(true);
        String lcbdSql = "INSERT INTO YW_LCBD (ID, BDBS, BDMC, LCLB, LJDZ, BZSM, SXH, WXLJDZ, MBLJ) VALUES ('" + shEditId + "', '" + packageName + "_" + className + "sp_edit', '编辑', '" + className + "sp', '/" + packageName + "/" + className + "/shEditFrame.do', null, 1, null, null);\n" +
                "INSERT INTO YW_LCBD (ID, BDBS, BDMC, LCLB, LJDZ, BZSM, SXH, WXLJDZ, MBLJ) VALUES ('" + shId + "', '" + packageName + "_" + className + "sp_sh', '审核', '" + className + "sp', '/" + packageName + "/" + className + "/shFrame.do', null, 2, null, null);\n" +
                "INSERT INTO YW_LCBD (ID, BDBS, BDMC, LCLB, LJDZ, BZSM, SXH, WXLJDZ, MBLJ) VALUES ('" + shQrId + "', '" + packageName + "_" + className + "sp_qr', '确认', '" + className + "sp', '/" + packageName + "/" + className + "/shQrFrame.do', null, 3, null, null);\n";
        // 流程管理

        String bzlcSql = "INSERT INTO YW_BZLC (ID, LCMC, LCJK, JSSJ, THSJ, TJSJ, LZSJ, FQSJ, SXH, LCLB, LCBD, LCBDBS, LCBDLX, SLJC, WXYS, DXTXBZ, DXNR, DZYJTXBZ, DZYJNR, DXZXJB, TJJBZXLB, CXSJ, CXSJZXJB, LCJSSJQZX, LCJSSJQZXJB, LCJSSJHZX, LCJSSJHZXJB, WXZSTP, LCJSY, YWSJKZBZ, LCJSFYZBZ, YZLCBD, YZLCBDBS, YZLCBDLX, DISAGREEZXJB, BBH) \n" +
                "VALUES ('" + lcmc + "', '" + comments + "审批', '" + lcmc + "_EVEN', 1, 1, 1, 1, 1, 99999, '" + className + "sp', '/" + packageName + "/" + className + "/shFrame.do', '" + packageName + "_" + className + "sp_sh', 2, '" + comments + "审批', '2', -1, null, -1, null, null, 1, 1, null, 1, null, 1, null, null, null, 1, -1, '/" + packageName + "/" + className + "/shFrame.do', '" + packageName + "_" + className + "sp_sh', 2, null, 1);\n";

        // 流程环节
        String fqhjId = UUID.randomUUID().toString(true);
        String sphjId1 = UUID.randomUUID().toString(true);
        String sphjId2 = UUID.randomUUID().toString(true);
        String lchj = "INSERT INTO YW_BZHJ (ID, BZLC, HJMC, FPFS, ZXDX, HJBS, HJLX, XZSJ, DQBDLX, DQBD, TGBDLX, TGBD, BTMC, THQX, YWZT, TJYWZT, SXH, MLEFT, TOP, PROCESS_TO, DQBDBS, TGBDBS, TJJB, BZSM, SCSLQX, SCSQQX, SPYJBTBZ, HJKJBZ, SPYJBT, HQLX, HQLSH, THYJBTBZ, DXTXBZ, DXNR, TJSJJYBZ, TJSJJYDM, DZYJTXBZ, DZYJNR, TJQRBZ, JSBZ, FJDSL, KJSCS, JSCS, ZJDSL, MRSPY, CGTSKZ, CGCZBTMC, SFZSBMMC, ZDLZTJJB, ZDLZHJKJBZ, FQZLCBZ, ZLCBH, ZLCFQJB, JSBTMC, TWTJKZ, XYJDKZ, JQSBZ, JQSHJMC, YWSJKZBZ, JQSBT, QHQX, SFXSSHR, JQSBDBS, JQSBDLX, JQSBD, JSLCQX, JSQTHQX, SHCHBTMC, ZDYNODEBZ, DBZJBZ, DBZJBT, BTYCZQX, BTYCZBTMC, THCZBTMC, SPJLXSBZ, DBTCTX, DBTCTXSJ, SFKYPLSP, JQSXZCS, BLYJCZQX, BLYJCZBTMC, SPFJSCBZ, YWSPHQGXQX, HJTSY, PROCESS_BACK, JQSRYFW, HQJSBZ, HQJSJB, SHTGCSDBBZ, RYTZKZ, SFXSDB, RGCSDBQX) \n" +
                "VALUES ('" + fqhjId + "', '" + lcmc + "', '发起', 1, 'admin', 'fq', 1, 0, 2, '/" + packageName + "/" + className + "/shFrame.do', 2, '/" + packageName + "/" + className + "/shFrame.do', '审核通过', 0, null, null, 1, '359px', '374px', '" + sphjId1 + "', '" + packageName + "_" + className + "_sh', '" + packageName + "_" + className + "_sh', null, '发起', -1, -1, 1, 1, '审批意见', 0, null, 1, -1, null, -1, null, -1, null, 1, 1, 0, 1, 1, 1, null, 1, '确定', -1, null, 0, -1, null, null, '审核结束', 2, 1, -1, null, -1, '加签/审', -1, 1, null, 2, null, -1, -1, '审核撤回', -1, -1, '转交待办', -1, '不同意', '退回', 1, -1, 0, -1, 0, -1, null, 1, -1, null, null, 1, 1, null, -1, -1, 1, -1);\n" +

                "INSERT INTO YW_BZHJ (ID, BZLC, HJMC, FPFS, ZXDX, HJBS, HJLX, XZSJ, DQBDLX, DQBD, TGBDLX, TGBD, BTMC, THQX, YWZT, TJYWZT, SXH, MLEFT, TOP, PROCESS_TO, DQBDBS, TGBDBS, TJJB, BZSM, SCSLQX, SCSQQX, SPYJBTBZ, HJKJBZ, SPYJBT, HQLX, HQLSH, THYJBTBZ, DXTXBZ, DXNR, TJSJJYBZ, TJSJJYDM, DZYJTXBZ, DZYJNR, TJQRBZ, JSBZ, FJDSL, KJSCS, JSCS, ZJDSL, MRSPY, CGTSKZ, CGCZBTMC, SFZSBMMC, ZDLZTJJB, ZDLZHJKJBZ, FQZLCBZ, ZLCBH, ZLCFQJB, JSBTMC, TWTJKZ, XYJDKZ, JQSBZ, JQSHJMC, YWSJKZBZ, JQSBT, QHQX, SFXSSHR, JQSBDBS, JQSBDLX, JQSBD, JSLCQX, JSQTHQX, SHCHBTMC, ZDYNODEBZ, DBZJBZ, DBZJBT, BTYCZQX, BTYCZBTMC, THCZBTMC, SPJLXSBZ, DBTCTX, DBTCTXSJ, SFKYPLSP, JQSXZCS, BLYJCZQX, BLYJCZBTMC, SPFJSCBZ, YWSPHQGXQX, HJTSY, PROCESS_BACK, JQSRYFW, HQJSBZ, HQJSJB, SHTGCSDBBZ, RYTZKZ, SFXSDB, RGCSDBQX) \n" +
                "VALUES ('" + sphjId1 + "', '" + lcmc + "', '业务审批', 1, 'f36334e5a5714e9c98048bad4a8f2a23', 'ywsp', 1, 0, 2, '/" + packageName + "/" + className + "/shFrame.do', 2, '/" + packageName + "/" + className + "/shFrame.do', '审核通过', 1, null, null, 2, '631px', '374px', '" + sphjId2 + "', '" + packageName + "_" + className + "_sh', '" + packageName + "_" + className + "_sh', null, '业务审批', -1, -1, 1, 1, '审批意见', 0, null, 1, -1, null, -1, null, -1, null, 1, 1, 1, 1, 1, 1, '同意', 1, '确定', -1, null, 0, -1, null, null, '审核结束', 2, 1, -1, null, -1, '加签/审', 1, 1, null, 2, null, -1, -1, '审核撤回', -1, -1, '转交待办', -1, '不同意', '退回', 1, -1, 0, -1, 0, -1, '保留意见', 1, -1, null, null, 1, 1, null, -1, -1, 1, -1);\n" +

                "INSERT INTO YW_BZHJ (ID, BZLC, HJMC, FPFS, ZXDX, HJBS, HJLX, XZSJ, DQBDLX, DQBD, TGBDLX, TGBD, BTMC, THQX, YWZT, TJYWZT, SXH, MLEFT, TOP, PROCESS_TO, DQBDBS, TGBDBS, TJJB, BZSM, SCSLQX, SCSQQX, SPYJBTBZ, HJKJBZ, SPYJBT, HQLX, HQLSH, THYJBTBZ, DXTXBZ, DXNR, TJSJJYBZ, TJSJJYDM, DZYJTXBZ, DZYJNR, TJQRBZ, JSBZ, FJDSL, KJSCS, JSCS, ZJDSL, MRSPY, CGTSKZ, CGCZBTMC, SFZSBMMC, ZDLZTJJB, ZDLZHJKJBZ, FQZLCBZ, ZLCBH, ZLCFQJB, JSBTMC, TWTJKZ, XYJDKZ, JQSBZ, JQSHJMC, YWSJKZBZ, JQSBT, QHQX, SFXSSHR, JQSBDBS, JQSBDLX, JQSBD, JSLCQX, JSQTHQX, SHCHBTMC, ZDYNODEBZ, DBZJBZ, DBZJBT, BTYCZQX, BTYCZBTMC, THCZBTMC, SPJLXSBZ, DBTCTX, DBTCTXSJ, SFKYPLSP, JQSXZCS, BLYJCZQX, BLYJCZBTMC, SPFJSCBZ, YWSPHQGXQX, HJTSY, PROCESS_BACK, JQSRYFW, HQJSBZ, HQJSJB, SHTGCSDBBZ, RYTZKZ, SFXSDB, RGCSDBQX) \n" +
                "VALUES ('" + sphjId2 + "', '" + lcmc + "', '管理员审批', 1, 'admin', 'glysp', 1, 0, 0, '/" + packageName + "/" + className + "/shFrame.do', 0, '/" + packageName + "/" + className + "/shFrame.do', '审核通过', 3, null, null, 3, '912px', '374px', null, '" + packageName + "_" + className + "_sh', '" + packageName + "_" + className + "_sh', null, '管理员审批', -1, -1, 1, 1, '审批意见', 0, null, 1, -1, null, -1, null, -1, null, 1, 1, 1, 1, 1, 0, '同意', 1, '确定', -1, null, 0, -1, null, null, '审核结束', 2, 1, -1, null, -1, '加签/审', 1, 1, null, 0, null, -1, -1, '审核撤回', -1, -1, '转交待办', -1, '不同意', '退回', -1, -1, 0, -1, 0, -1, '保留意见', 1, -1, null, null, 1, 1, null, -1, -1, 1, -1);\n" +

                "INSERT INTO YW_BZZF (ID, BZLC, BZHJ, ZFHJ, TJSJ, ZFJB, ZFSM) \n" +
                "VALUES ('" + UUID.randomUUID().toString(true) + "', '" + lcmc + "', '" + fqhjId + "', '" + sphjId1 + "', 20220215145027, null, null);\n" +

                "INSERT INTO YW_BZZF (ID, BZLC, BZHJ, ZFHJ, TJSJ, ZFJB, ZFSM) \n" +
                "VALUES ('" + UUID.randomUUID().toString(true) + "', '" + lcmc + "', '" + sphjId1 + "', '" + sphjId2 + "', 20230303152206, null, null);\n";

        String jscd = "INSERT INTO GG_JSGN (ID, XTJS, GNID, SXH) VALUES ('" + UUID.randomUUID().toString(true) + "', 'XT_XTGLY', '" + xtgnUuid + "', 0);";

        System.out.println(xtgnSql);
        if (StrUtil.isNotEmpty(lcmc)) {
            System.out.println(splcSql);
            System.out.println(lcbdSql);
            System.out.println(bzlcSql);
            System.out.println("--以下的不需要提交\n");
            System.out.println(lchj);
        }
        System.out.println(jscd);
    }
}
